package com.fengchao.api.service;

import com.fengchao.api.config.Constant;
import com.fengchao.api.dao.bci.BciArtistInfoMapper;
import com.fengchao.api.dao.bci.BciFieldInfoMapper;
import com.fengchao.api.dao.cfg.CfgChinaCityListMapper;
import com.fengchao.api.dao.cfg.CfgRegionMapper;
import com.fengchao.api.dao.pfm.PfmArtistJoinMapper;
import com.fengchao.api.dao.pfm.PfmPerformanceSingleMapper;
import com.fengchao.api.dao.pfm.PfmPerformanceTicketMapper;
import com.fengchao.api.dao.pfm.PfmPerformanceTotalMapper;
import com.fengchao.api.dao.usr.UsrUserFavoriteMapper;
import com.fengchao.model.bci.BciArtistInfo;
import com.fengchao.model.bci.BciFieldInfo;
import com.fengchao.model.cfg.CfgChinaCityList;
import com.fengchao.model.cfg.CfgRegion;
import com.fengchao.model.entity.comm.CustomBizException;
import com.fengchao.model.pfm.PfmArtistJoin;
import com.fengchao.model.pfm.PfmPerformanceSingle;
import com.fengchao.model.pfm.PfmPerformanceTicket;
import com.fengchao.model.pfm.PfmPerformanceTotal;
import com.fengchao.model.usr.UsrUserFavorite;
import com.fengchao.util.common.CommonValidator;
import com.fengchao.util.common.DateUtil;
import com.fengchao.util.common.PinyinTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.fengchao.api.config.Constant.GET_FAILURE;
import static com.sun.tools.javac.jvm.ByteCodes.pop;

/**
 * Created by http://my.oschina.net/wangnian on 2016/9/8.
 */
@Service
public class PfmService {

    @Autowired
    PfmPerformanceTotalMapper pfmPerformanceTotalMapper;
    @Autowired
    BciFieldInfoMapper bciFieldInfoMapper;
    @Autowired
    PfmArtistJoinMapper pfmArtistJoinMapper;
    @Autowired
    BciArtistInfoMapper bciArtistInfoMapper;
    @Autowired
    PfmPerformanceSingleMapper pfmPerformanceSingleMapper;
    @Autowired
    PfmPerformanceTicketMapper pfmPerformanceTicketMapper;
    @Autowired
    CfgRegionMapper cfgRegionMapper;
    @Autowired
    UsrUserFavoriteMapper usrUserFavoriteMapper;
    @Autowired
    UsrService usrService;
    @Autowired
    CfgChinaCityListMapper cfgChinaCityListMapper;


    /**
     * 获取演唱会的列表
     *
     * @param cityId
     * @param fieldType
     * @param musicStyle
     * @param nearTime
     * @param page
     * @param count
     */
    public List<Map<String, Object>> getPerformanceList(Integer userId, Integer cityId, String fieldType, String musicStyle, String nearTime, Integer page, Integer count) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Integer start = (page - 1) * count;
        //根据最近的时间算
        Date date = new Date();
        //ONEWEEK 近一周/ONEMONTH 近一个月/THREEMONTHS 近三个月/HALFYEAR 近半年
        switch (nearTime) {
            case "ONEWEEK":
                date = DateUtil.addDay(date, 7);
                break;
            case "ONEMONTH":
                date = DateUtil.addMonth(date, 1);
                break;
            case "THREEMONTHS":
                date = DateUtil.addMonth(date, 3);
                break;
            case "HALFYEAR":
                date = DateUtil.addMonth(date, 6);
                break;
            default:
                date = DateUtil.addMonth(date, 12);//默认是近一年的
                break;
        }
        List<PfmPerformanceTotal> pfmPerformanceTotals = pfmPerformanceTotalMapper.findTotalListByCityIdAndfieldTypeAndMusicStyleAndReleaseTime(cityId, fieldType, musicStyle, date, start, count);
        for (PfmPerformanceTotal pfmPerformanceTotal : pfmPerformanceTotals) {
            BciFieldInfo bciFieldInfo = bciFieldInfoMapper.selectByPrimaryKey(pfmPerformanceTotal.getFieldId());
            Map<String, Object> resuletMap = new HashMap();
            resuletMap.put("pfmId", pfmPerformanceTotal.getId());
            resuletMap.put("musicStyle", pfmPerformanceTotal.getMusicStyle());
            resuletMap.put("pfmTitle", pfmPerformanceTotal.getPerformanceTitle());
            resuletMap.put("pfmImageUrl", pfmPerformanceTotal.getPfmRectangleImageUrl());
            resuletMap.put("fieldName", bciFieldInfo == null ? "" : bciFieldInfo.getFieldName());
            resuletMap.put("priceRange", pfmPerformanceTotal.getPriceRange());
            resuletMap.put("pfmTimeRange", pfmPerformanceTotal.getPfmTimeRange());
            resuletMap.put("pfmTime", pfmPerformanceTotal.getPfmTime());
            resuletMap.put("pfmStatus", pfmPerformanceTotal.getStatus());
            // 查询用户是否收藏
            Integer isFavorite = 0;
            //是否收藏
            if (CommonValidator.isStringsNotNull(userId)) {
                List<UsrUserFavorite> usrUserFavorites = usrService.findUsrUserFavoriteByTypeUserIdForeignId(Constant.PERFORMANCE, userId, pfmPerformanceTotal.getId());
                if (usrUserFavorites.size() > 0) {
                    isFavorite = 1;
                }
            }
            resuletMap.put("isFavorite", isFavorite);
            //分享地址
            resuletMap.put("shareUrl", Constant.CONFIGMAP.get("pfmShare") + pfmPerformanceTotal.getId());
            resultList.add(resuletMap);
        }
        return resultList;
    }

    /**
     * 获取演唱会的信息和须知
     *
     * @param pfmId
     * @param category
     * @return
     */
    public String getpfmInfoBuyNotice(Integer pfmId, String category) {
        PfmPerformanceTotal pfmPerformanceTotal = pfmPerformanceTotalMapper.selectByPrimaryKey(pfmId);
        if ("INFO".equals(category)) {
            return pfmPerformanceTotal.getPfmInfo();
        } else {
            return pfmPerformanceTotal.getBuyNotice();
        }
    }

    /**
     * 获取详情
     *
     * @param pfmId
     * @param userId
     * @return
     */
    public Map<String, Object> getPfmInfo(Integer pfmId, Integer userId) {
        Map<String, Object> resuletMap = new HashMap();
        PfmPerformanceTotal pfmPerformanceTotal = pfmPerformanceTotalMapper.selectByPrimaryKey(pfmId);
        if (pfmPerformanceTotal == null) {
            throw new CustomBizException(GET_FAILURE);
        }
        resuletMap.put("pfmTitle", pfmPerformanceTotal.getPerformanceTitle());
        resuletMap.put("pfmImageUrl", pfmPerformanceTotal.getPfmRectangleImageUrl());
        resuletMap.put("pfmSmallImageUrl", pfmPerformanceTotal.getPfmSquareImageUrl());
        resuletMap.put("pfmTimeRange", pfmPerformanceTotal.getPfmTimeRange());
        resuletMap.put("priceRange", pfmPerformanceTotal.getPriceRange());
        // 分享地址  详细  须知
        resuletMap.put("shareUrl", Constant.CONFIGMAP.get("pfmShare") + pfmPerformanceTotal.getId());
        resuletMap.put("buyNoticeUrl", Constant.CONFIGMAP.get("pfmNoteUrl") + pfmPerformanceTotal.getId());
        resuletMap.put("pfmInfoUrL", Constant.CONFIGMAP.get("pfmInfoUrl") + pfmPerformanceTotal.getId());
        //查询场地信息
        BciFieldInfo bciFieldInfo = bciFieldInfoMapper.selectByPrimaryKey(pfmPerformanceTotal.getFieldId());
        resuletMap.put("fieldName", bciFieldInfo == null ? "" : bciFieldInfo.getFieldName());
        resuletMap.put("fieldAdress", bciFieldInfo == null ? "" : bciFieldInfo.getFieldAddress());
        resuletMap.put("longitude", bciFieldInfo == null ? "" : bciFieldInfo.getLongitude());
        resuletMap.put("latitude", bciFieldInfo == null ? "" : bciFieldInfo.getLatitude());
        // 查询用户是否收藏
        Integer isFavorite = 0;
        //是否收藏
        if (CommonValidator.isStringsNotNull(userId)) {
            List<UsrUserFavorite> usrUserFavorites = usrService.findUsrUserFavoriteByTypeUserIdForeignId(Constant.PERFORMANCE, userId, pfmPerformanceTotal.getId());
            if (usrUserFavorites.size() > 0) {
                isFavorite = 1;
            }
        }
        resuletMap.put("isFavorite", isFavorite);
        //查询关联的艺人信息
        List<Map<String, Object>> artistList = new ArrayList<>();
        List<PfmArtistJoin> pfmArtistJoinList = pfmArtistJoinMapper.findPfmArtistJoinBypfmId(pfmPerformanceTotal.getId());
        for (PfmArtistJoin pfmArtistJoin : pfmArtistJoinList) {
            BciArtistInfo bciArtistInfo = bciArtistInfoMapper.selectByPrimaryKey(pfmArtistJoin.getArtistId());
            Map<String, Object> artistMap = new HashMap();
            artistMap.put("artistId", bciArtistInfo == null ? "" : bciArtistInfo.getId());
            artistMap.put("artistDisplay", bciArtistInfo == null ? "" : bciArtistInfo.getNameDisplay());
            artistMap.put("nationality", bciArtistInfo == null ? "" : bciArtistInfo.getNationality());
            artistMap.put("headIconApp", bciArtistInfo == null ? "" : bciArtistInfo.getHeadIconApp());
            artistList.add(artistMap);
        }
        resuletMap.put("pfmTime", pfmPerformanceTotal.getPfmTime());
        resuletMap.put("artistList", artistList);
        return resuletMap;
    }


    /**
     * 获取购买项
     *
     * @param pfmId
     * @return
     */
    public List<Map<String, Object>> getPurchaseItems(Integer pfmId) {
        // Map<String, Object> resultMap=new HashMap<>();
        PfmPerformanceTotal pfmPerformanceTotal = pfmPerformanceTotalMapper.selectByPrimaryKey(pfmId);
        //查询场次
        List<Map<String, Object>> singleResultList = new ArrayList<>();
        List<PfmPerformanceSingle> pfmPerformanceSingleList = pfmPerformanceSingleMapper.findByPfmId(pfmId);
        for (PfmPerformanceSingle pfmPerformanceSingle : pfmPerformanceSingleList) {
            Map<String, Object> singleResultMap = new HashMap<>();
            singleResultMap.put("pfmSingleId", pfmPerformanceSingle.getId());//场次id
            singleResultMap.put("pfmSingleName", pfmPerformanceSingle.getPfmSingleName());//场地名字
            singleResultMap.put("pfmSingleTime", pfmPerformanceSingle.getPfmSingleTime());//场地的时间
            singleResultMap.put("memo", pfmPerformanceSingle.getMemo());
            //根据场地查询票种
            List<Map<String, Object>> ticketResultList = new ArrayList<>();
            List<PfmPerformanceTicket> pfmPerformanceTickets = pfmPerformanceTicketMapper.findBysingleId(pfmPerformanceSingle.getId());
            for (PfmPerformanceTicket pfmPerformanceTicket : pfmPerformanceTickets) {
                Map<String, Object> ticketMap = new HashMap<>();
                ticketMap.put("ticketName", pfmPerformanceTicket.getPfmTicketName());
                ticketMap.put("ticketPrice", pfmPerformanceTicket.getPfmTicketPrice());
                ticketMap.put("ticketId", pfmPerformanceTicket.getId());
                ticketResultList.add(ticketMap);
            }
            singleResultMap.put("ticketList", ticketResultList);
            singleResultList.add(singleResultMap);
        }
        return singleResultList;
    }

    /**
     * 获取城市
     *
     * @return
     */
    public Map<String, List> getPfmCity() {
        Map<String, List> resultMap = new HashMap<>();
        List<Map<String, Object>> popList = new ArrayList<>();
        List<CfgChinaCityList> cfgChinaCityLists = cfgChinaCityListMapper.findByRecommanded();
        for (CfgChinaCityList cfgChinaCityList : cfgChinaCityLists) {
            Map<String, Object> pop = new HashMap<>();
            pop.put("id", cfgChinaCityList.getCityId());//城市id
            pop.put("name", cfgChinaCityList.getCityName());//城市名
            pop.put("cityCode", cfgChinaCityList.getCityCode());//城市code
            pop.put("code","0");//区code
            popList.add(pop);
        }
        List<CfgChinaCityList> cfgChinaCityLists1 = cfgChinaCityListMapper.findIsDisplay();
        List<Map<String, Object>> cityList = new ArrayList<>();
        Map<String, List<Map>> map = new TreeMap<>();
        for (CfgChinaCityList cfgChinaCityList : cfgChinaCityLists1) {
            List<Map> list = map.get(cfgChinaCityList.getCityGroupAbbr());
            Map<String, Object> cityMap = new HashMap<>();
            cityMap.put("id", cfgChinaCityList.getCityId());
            cityMap.put("name", cfgChinaCityList.getCityName());
            cityMap.put("cityCode", cfgChinaCityList.getCityCode());//城市code
            cityMap.put("code","0");//区code
            if (list == null) {
                list = new ArrayList<>();
                list.add(cityMap);
                map.put(cfgChinaCityList.getCityGroupAbbr(), list);
            } else {
                list.add(cityMap);
            }
        }
        for (String key : map.keySet()) {
            Map<String, Object> tempMap = new HashMap<>();
            tempMap.put("letter", key.toUpperCase());
            tempMap.put("letterList", map.get(key));
            cityList.add(tempMap);
        }
        resultMap.put("cityList", cityList);
        resultMap.put("popList", popList);
        return resultMap;
    }

    /**
     * 获取收藏的演出
     *
     * @param userId
     * @param page
     * @param count
     * @return
     */
    public List<Map> getFavoritePerformace(Integer userId, Integer page, Integer count) {
        Integer start = (page - 1) * count;
        return pfmPerformanceTotalMapper.findFavoritePerformanceList(userId, start, count);
    }


}
